package com.ljq.cksp.controller;

import com.ljq.cksp.dao.UserDAO;
import com.ljq.cksp.pojo.Result;
import com.ljq.cksp.pojo.StatusCode;
import com.ljq.cksp.pojo.User;
import com.ljq.cksp.service.UserService;
import com.ljq.cksp.util.MailUtil;
import com.ljq.cksp.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Random;

/**
 * @Author:ljq
 * @Date:2019/12/8
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    MailUtil mailUtil;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    PasswordEncoder passwordEncoder;

    @PostMapping()
    public Result register(User user, String captcha){
        Result result = new Result();
        String username =  user.getName();
        String oldCaptcha = redisUtil.get(user.getEmail());
        if(oldCaptcha == null){
            result.setStatus(StatusCode.ERROR);
            result.setMsg("验证码不存在或过期");
        }else if(!captcha.equals(oldCaptcha)){
            result.setStatus(StatusCode.ERROR);
            result.setMsg("验证码错误");
        }else if(userService.nameIsExist(username)) {
            result.setStatus(StatusCode.ERROR);
            result.setMsg("用户名已经被使用,不能使用");
        }else{
            userService.register(user);
            result.setMsg("注册成功，请登录");
        }
        return result;
    }

    @GetMapping("/{userId}")
    public Result getUser(@PathVariable("userId")int userId){
        Result result = new Result();
        result.setData(userService.findById(userId));
        return result;
    }

    @PostMapping("/{userId}")
    public Result modifyUser(@PathVariable("userId")int userId, User user){
        Result result = new Result();
        user = userService.modify(userId, user);
        result.setData(user);
        result.setMsg("信息修改成功");
        return result;
    }

    @PostMapping("/{userId}/password")
    public Result modifyPassword(@PathVariable("userId")int userId, String oldPassword, String password){
        Result result = new Result();
        User user = userService.modifyPassword(userId, password);
        result.setData(user);
        result.setMsg("密码修改成功");
        return result;
    }

    @PostMapping("/captcha")
    public Result registerCaptcha(String email) {
        Result result = new Result();
        if(userService.findByEmail(email)!= null){
            result.setMsg("该邮箱已被注册");
            result.setStatus(StatusCode.ERROR);
            return result;
        }
        String verifyCode=String.valueOf(new Random().nextInt(899999) + 100000);
        redisUtil.set(email, verifyCode);
        mailUtil.sendMail(email, "您正在注册法律知识服务平台账号，您的验证码为："+verifyCode);
        result.setMsg("验证码发送成功");
        return result;
    }

    @PostMapping("/password/captcha")
    public Result passwordCaptcha(String email) {
        Result result = new Result();
        if(userService.findByEmail(email) == null){
            result.setMsg("该邮箱未注册");
            return result;
        }
        String verifyCode=String.valueOf(new Random().nextInt(899999) + 100000);
        redisUtil.set(email, verifyCode);
        mailUtil.sendMail(email, "您正在使用法律知识平台的密码找回服务，您的验证码为："+verifyCode);
        result.setMsg("验证码发送成功");
        return result;
    }

    @PostMapping("/password")
    public Result modifyPasswordByCaptcha(String email, String password, String captcha) {
        Result result = new Result();
        String oldCaptcha = redisUtil.get(email);
        if(oldCaptcha == null){
            result.setMsg("验证码不存在或过期");
        }else if(!captcha.equals(oldCaptcha)){
            result.setMsg("验证码错误");
        }else{
            User user = userService.findByEmail(email);
            userService.modifyPassword(user.getId(), password);
            result.setMsg("密码修改成功");
        }
        return result;
    }
}
